package A1_二叉树;

import java.util.ArrayList;
import java.util.List;

public class LC_144前序遍历 {
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(int val){
            this.val=val;
        }
        TreeNode(int val,TreeNode left,TreeNode right){
            this.val=val;
            this.left=left;
            this.right=right;
        }
    }
    public static void main(String[] args){
        TreeNode root=new TreeNode(1);
//        root.left=new TreeNode();
        root.right=new TreeNode(2);
        root.right.left=new TreeNode(3);
//        TreeNode root= new int[]{1, , 2, 3};
        System.out.println(preorderTraversal(root));
    }

    public static List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        preorder(root,list);
        return list;
    }
    public static void preorder(TreeNode cur,List<Integer> list){
        if(cur==null)
            return;
        list.add(cur.val);
        preorder(cur.left,list);
        preorder(cur.right,list);
    }
}
